package com.dbz.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dbz.entity.User;
import com.dbz.entity.UserRoles;
import com.dbz.service.JournalService;
import com.dbz.service.ModulesService;
import com.dbz.service.UserRolesService;
import com.dbz.service.UserService;
import com.dbz.util.JwtUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.*;

@RequestMapping("/user/")
@RestController
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private ModulesService modulesService;
    @Resource
    private UserRolesService userRolesService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private JournalService journalService;
    @RequestMapping("login")
    public Map login(String userName, String password, Model model){
        String url=request.getRequestURI();
        String scheme=request.getScheme();
        Map<String,Object> map=new HashMap<>();
        User user =userService.getUserByName(userName);
             if(user==null){
                 map.put("no1","用户不存在");
                 return map;
             }else {
                 if (user.getUpassword().equals(password)){
                     String token= JwtUtil.createToken(userName);
                     map.put("insertJournal",journalService.insertJournal(userName,url,scheme));
                     map.put("userName",user.getUname());
                     map.put("token",token);
                     map.put("success",true);
                     return map;
                 }else {
                     map.put("no2","密码错误");
                     return map;
                 }
             }
        //return map;
    }

    /**
     * @return
     */


    @RequestMapping("selectUser")
    public List<User> selectUser(){
        return  userService.selectUser();
    }

    @RequestMapping("modulesList")
    public Map modulesList(String userName){
        Map<String,Object> map=new HashMap<>();
        map.put("modulesList",modulesService.selectModelByUserName(userName));
        return  map;
    }

    /**
     * 删除
     * @param uid
     * @return
     */
    @RequestMapping("del")
    public int del(String uid){
        return userService.deleteUserById(uid);
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping("update")
    public  Map update(User user){
        Map<String,Object> map=new HashMap<>();
        List<User> users=userService.getAll();
       User userServiceUserByName=userService.selectUserById(user.getUid());
        for (User userss:users) {
            if (user.getUname().equals(userss.getUname())){
                System.out.println("55555");
                if (user.getUname().equals(userServiceUserByName.getUname())){
                    userService.updateRoleById(user);
                    map.put("success",true);
                    map.put("msg","操作成功");
                    System.out.println("6666");
                    return map;
                }}
                map.put("success",false);
                map.put("msg","名称已存在");
                return map;

        }
        map.put("success",true);
        map.put("msg","操作成功");
        userService.updateRoleById(user);
        return map;
    }
    /**
     * 添加
     * @param user
     * @return
     */
    @RequestMapping("add")
    public Map add(User user){
        user.setUid(UUID.randomUUID().toString());
        Map<String,Object> map=new HashMap<>();
        List<User> users=userService.getAll();
        for (User usera:users) {
            if (user.getUname().equals(usera.getUname())){
                map.put("msg","名称已经存在");
                map.put("success",false);
                return  map;
            }
        }
        map.put("success",true);
        map.put("msg","添加成功");
        userService.userAdd(user);
        return map;
    }
@RequestMapping("selectPageByNameBy")
    public  Map pageList(String uname,
                           @DateTimeFormat(pattern = "yyyy-MM-dd")
                           @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
                                   Date createTime,
                           @DateTimeFormat(pattern = "yyyy-MM-dd")
                           @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
                                   Date endTime,Integer page,Integer rows){
         Map<String,Object> map=new HashMap<>();
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
        queryWrapper.like(!uname.equals("")&&uname!=null,"uname",uname);
        queryWrapper.gt(createTime!=null,"creatTime",createTime);
        queryWrapper.lt(endTime!=null,"creatTime",endTime);
        IPage<User> pages=new Page<User>(page,rows);
            /*IPage<User> userIPage = userService.lambdaQuery()
            .like(StringUtils.isNotEmpty(uname),User::getUname,uname)
            .gt(uname!=null,User::getCreatTime,createTime)
            .lt(uname!=null,User::getCreatTime,endTime)
            .page(pages);*/
         //= userService.selectPage(pages, queryWrapper);
           map.put("rows", userService.page(pages,queryWrapper).getRecords());
           map.put("total",userService.page(pages,queryWrapper).getTotal());
        return  map;
    //return  map;
    }

    @RequestMapping("isUserAddJueSe")
    public  int isUserAddJueSe(UserRoles userRoles){
        System.out.println(UUID.randomUUID().toString());
        userRoles.setUrid(UUID.randomUUID().toString());
        userRolesService.isUserAddJueSe(userRoles);
        return 1;
    }
    @RequestMapping("delUserRolesByUidAndRid")
    public int delUserRolesByUidAndRid(String uid,String rid){
        userService.delUserRolesByUidAndRid(uid,rid);
        return 1;
    }
}
